package com.ccys.cms.controller.productAndService;


import com.ccys.cms.service.productAndService.MedicalServiceListDetailService;
import com.ccys.cms.service.productAndService.MedicalServiceListService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.productAndService.MedicalServiceListDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblMedicalServiceList;
import com.ccys.db.entity.TblMedicalServiceListDetail;
import com.ccys.db.vo.web.productAndService.MedicalServiceListVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "医疗服务集合")
public class MedicalServiceListController {


    @Resource
    private MedicalServiceListService service;

    @Resource
    private MedicalServiceListDetailService detailService;

    @PostMapping("medicalServiceList")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(TblMedicalServiceList model) {

        int i = service.insertOrUpdate(model);
        return ResponseResult.update(i);
    }

    @PutMapping("medicalServiceList")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(TblMedicalServiceList model) {

        int i = service.insertOrUpdate(model);
        return ResponseResult.update(i);
    }




    @DeleteMapping("medicalServiceList")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblMedicalServiceList model = new TblMedicalServiceList();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //删除listDetail
        detailService.deleteDetails(id);
        return ResponseResult.update(i);
    }

    @PostMapping("medicalServiceList/clone")
    @ApiOperation("克隆")
    @Transactional
    public ResponseResult clone(String id, String name) {

        TblMedicalServiceList entityById = service.findEntityById(id);
        entityById.setId(null);
        entityById.setName(name);
        int i = service.insertOrUpdate(entityById);
        String listId = entityById.getId();
        //查询原来detailList
        List<TblMedicalServiceListDetail> detailList = detailService.findListByLimit(TblMedicalServiceListDetail.class, new ArrayList<String>() {
            {
                add(" del_flag = 0 ");
                add(" list_id = '" + id + "'");
            }
        }, "crearted_on desc");
        detailList.forEach(x -> {
            x.setId(null);
            x.setListId(listId);
        });
        detailService.insertAll(detailList);
        return ResponseResult.update(i);
    }


    @GetMapping("medicalServiceList")
    @ApiOperation(value = "查询分页", notes = "排序字段 t1.name / t1.status /t2. user_fullname / t1.created_on")
    public ResponseResult<PageInfo<MedicalServiceListVo>, Object> getPage(MedicalServiceListDto dto) {

        return service.getPage(dto);
    }
}
